﻿//-----------------------------------------------------------------------
// <copyright file="Copyright.cs" author="Michael Herndon">
//     Copyright (c) Michael Herndon.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Amplify.Twitter
{
	using System;
	using System.ServiceModel.Security;


	/// <summary>
	/// Manages creating, deleting, and viewing direct messages between twitter users.
	/// </summary>
	public class DirectMessageClient : TwitterClient<IDirectMessageClient>,
		IDirectMessageClient 
	{


		/// <summary>
		/// Initializes a new instance of the <see cref="DirectMessageClient"/> class.
		/// </summary>
		/// <param name="username">The username.</param>
		/// <param name="password">The password.</param>
		public DirectMessageClient(string username, string password)
			: base(username, password)
		{
		}

		#region IDirectMessageClient Members

		/// <summary>
		/// Gets an array of the direct messages sent <em>to</em> the user.
		/// </summary>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetList()
		{
			return this.GetList(new DefaultOptions());
		}

		/// <summary>
		/// Gets an array of the direct messages sent <em>to</em> the user.
		/// </summary>
		/// <param name="options"> The <see cref="Amplify.Twitter.DefaultOptions"/> for getting the list. </param>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetList(DefaultOptions options)
		{
			try 
			{
				return this.Proxy.GetList(
							options.Page,
							options.SinceId,
							options.SinceValue);
			}
			catch (MessageSecurityException securityException)
			{
				this.Rescue(securityException);
			}
			return null;
		}

		/// <summary>
		/// Gets an array of the direct messages sent <em>to</em> the user.
		/// </summary>
		/// <param name="page">The page number (for paging). </param>
		/// <param name="since">Specify a search date for all the friends that have joined since.  </param>
		/// <param name="sinceId">Specify a integer for all ids to be greater than. </param>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetList(int page, int sinceId, string since)
		{
			try
			{
				return this.Proxy.GetList(page, sinceId, since);
			}
			catch(MessageSecurityException securityException) 
			{
				this.Rescue(securityException);
			}
			return null;

		}


		/// <summary>
		/// Gets an array of the direct messages sent <em>by</em> the user.
		/// </summary>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetSentList()
		{
			return this.GetList(new DefaultOptions());
		}

		/// <summary>
		/// Gets an array of the direct messages sent <em>by</em> the user.
		/// </summary>
		/// <param name="options"> The <see cref="Amplify.Twitter.DefaultOptions"/> options to get the list. </param>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetSentList(DefaultOptions options)
		{
			try
			{
				return this.Proxy.GetList(
						options.Page,
						options.SinceId,
						options.SinceValue);
			}
			catch (MessageSecurityException securityException)
			{
				this.Rescue(securityException);
			}
			return null;
		}

		/// <summary>
		/// Gets an array of the direct messages sent <em>by</em> the user.
		/// </summary>
		/// <param name="page">The page number (for paging). </param>
		/// <param name="since">Specify a search date for all the friends that have joined since.  </param>
		/// <param name="sinceId">Specify a integer for all ids to be greater than. </param>
		/// <returns>returns an array of <see cref="Amplify.Twitter.DirectMessage"/>. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage[] GetSentList(int page, int sinceId, string since)
		{
			try
			{
				return this.Proxy.GetSentList(page, sinceId, since);
			}
			catch (MessageSecurityException securityException)
			{
				this.Rescue(securityException);
			}
			return null;
		}

		/// <summary>
		/// Creates a Direct Message to the specified user.
		/// </summary>
		/// <param name="id">The screen name or user id of whom you want to send the message to.</param>
		/// <param name="text">The textual content of what you wish to send.</param>
		/// <returns>returns a <see cref="Amplify.Twitter.DirectMessage"/> object.</returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		/// <exception cref="Amplify.Twitter.TwitterException"> The text can not be longer than 140 characters. </exception>
		public DirectMessage Create(string id, string text)
		{
			if (text != null && text.Length > 140)
				throw new TwitterException("the text message can not be greater than 140 characters");

			try
			{
				return this.Proxy.Create(id, text);
			}
			catch (MessageSecurityException securityException)
			{
				this.Rescue(securityException);
			}
			return null;
		}


		/// <summary>
		/// Destroys only a Direct Message <strong>sent by another user to the current user</strong>.
		/// </summary>
		/// <param name="messageId">The id of the specified message to be deleted.</param>
		/// <returns>returns a <see cref="Amplify.Twitter.DirectMessage"/> object. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage Destroy(int messageId)
		{
			return this.Destroy(messageId.ToString());
		}

		/// <summary>
		/// Destroys only a Direct Message <strong>sent by another user to the current user</strong>.
		/// </summary>
		/// <param name="messageId">The id of the specified message to be deleted.</param>
		/// <returns>returns a <see cref="Amplify.Twitter.DirectMessage"/> object. </returns>
		/// <exception cref="System.ServiceModel.Security.MessageSecurityException" />
		///	<exception cref="Amplify.Twitter.TwitterException"> Throws when a twitter error occurs. </exception>
		public DirectMessage Destroy(string messageId)
		{
			try
			{
				return this.Proxy.Destroy(messageId);
			}
			catch (MessageSecurityException securityException)
			{
				this.Rescue(securityException);
			}
			return null;
		}

		#endregion

	}
}
